package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateSelectResearch
//@description: 创建SelectResearch记录
//@param: selectResearch model.SelectResearch
//@return: err error

func CreateSelectResearch(selectResearch model.SelectResearch) (err error) {
	id, err := GetLastSelectResearchID()
	if err != nil {
		selectResearch.SelectResearchId = model.SelectRearchIdPrefix + 1
	}else{
		selectResearch.SelectResearchId = id + 1
	}
	err = global.GVA_DB.Create(&selectResearch).Error
	return err
}

/**
获取最新的ID
*/
func GetLastSelectResearchID() (int, error) {
	var selectResearchM model.SelectResearch
	db := global.GVA_DB.Model(&model.SelectResearch{})
	err := db.Last(&selectResearchM).Error
	return selectResearchM.SelectResearchId, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSelectResearch
//@description: 删除SelectResearch记录
//@param: selectResearch model.SelectResearch
//@return: err error

func DeleteSelectResearch(selectResearch model.SelectResearch) (err error) {
	err = global.GVA_DB.Delete(&selectResearch).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSelectResearchByIds
//@description: 批量删除SelectResearch记录
//@param: ids request.IdsReq
//@return: err error

func DeleteSelectResearchByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.SelectResearch{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSelectResearch
//@description: 更新SelectResearch记录
//@param: selectResearch *model.SelectResearch
//@return: err error

func UpdateSelectResearch(selectResearch model.SelectResearch) (err error) {
	err = global.GVA_DB.Save(&selectResearch).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSelectResearch
//@description: 根据id获取SelectResearch记录
//@param: id uint
//@return: err error, selectResearch model.SelectResearch

func GetSelectResearch(id uint) (err error, selectResearch model.SelectResearch) {
	err = global.GVA_DB.Where("id = ?", id).First(&selectResearch).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSelectResearchInfoList
//@description: 分页获取SelectResearch记录
//@param: info request.SelectResearchSearch
//@return: err error, list interface{}, total int64

func GetSelectResearchInfoList(info request.SelectResearchSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.SelectResearch{})
    var selectResearchs []model.SelectResearch
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&selectResearchs).Error
	return err, selectResearchs, total
}